package net.zjitc.service.impl;

import net.zjitc.dao.CartDao;
import net.zjitc.dao.impl.CartDaoImpl;
import net.zjitc.pojo.CartItem;
import net.zjitc.service.CartService;
import net.zjitc.utils.CommonUtils;

import java.util.List;

/**
 * Created by Administrator on 2017/12/11.
 */
public class CartServiceImpl implements CartService {
    CartDao cartDao = new CartDaoImpl();
    public List<CartItem> findByUid(int uid) {
        return cartDao.findByUid(uid);
    }

    public int add(CartItem cartItem) {
        CartItem _cartitem = cartDao.findByPidAndUid(cartItem.getProduct().getPid(),cartItem.getUser().getUid());
        if(_cartitem == null){
            cartItem.setCartid(CommonUtils.uuid());
            return cartDao.add(cartItem);
        }else {
            int quantity = cartItem.getQuantity()+_cartitem.getQuantity();
            _cartitem.setQuantity(quantity);
            return cartDao.update(_cartitem);
        }
    }

    public int deletecart(String cartid) {
        return cartDao.deletecart(cartid);
    }

    public void deleteByUid(int uid) {
         cartDao.deleteByUid(uid);
    }

    public int clear(int uid) {
        return cartDao.clear(uid);
    }
}
